﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using T.ServiceComponent.Cluster.Client;
using T.ServiceFramework.Spi;

namespace T.SF.Console
{
    internal class AppConfig
    {
        private static bool isConfigCenter = false;
        private static IProviderConfigManager manager = null;
        private static string hostID = string.Empty;

        static AppConfig()
        {
            isConfigCenter = IsConfigCenter();
            if (isConfigCenter == true)
                manager = new ConfigCenterManager();
            else
                manager = new AppConfigManager();
        }

        /// <summary>
        /// 只允许外部在程序启动时调用一次
        /// </summary>
        /// <param name="startupHostId"></param>
        internal static void SetHostId(string startupHostId)
        {
            if (!string.IsNullOrWhiteSpace(startupHostId) && string.IsNullOrWhiteSpace(hostID))
            {
                hostID = startupHostId;
            }
        }

        /// <summary>
        /// 是否本地模式
        /// </summary>
        /// <returns></returns>
        internal static bool IsLocalModel()
        {
            string isLocalModel = T.ServiceComponent.Kernel.Service.ConfigHelper.GetString("IsLocalModel");

            return string.Equals("true", isLocalModel, StringComparison.OrdinalIgnoreCase) || string.Equals("1", isLocalModel, StringComparison.OrdinalIgnoreCase);
        }

        private static bool IsConfigCenter()
        {
            string enableConfigCenter = T.ServiceComponent.Kernel.Service.ConfigHelper.GetString("ConfigCenter.IsEnabled");

            if (string.IsNullOrEmpty(enableConfigCenter))
                enableConfigCenter = "false";
            else if (enableConfigCenter == "1")
                enableConfigCenter = "true";

            bool result = false;
            bool.TryParse(enableConfigCenter, out result);

            return result;
        }

        internal static string GetHostIDInternal()
        {
            string hostId = T.ServiceComponent.Kernel.Service.ConfigHelper.GetString("HostID");

            if (string.IsNullOrEmpty(hostId))
                hostId = AppClusterLocalClient.GetInstance().HostID;
            else
                TSFLogger.Warn("Get HostID From Local AppSettings Success:" + hostId);

            return hostId;
        }

        internal static string GetHostID()
        {
            if (string.IsNullOrWhiteSpace(hostID))
            {
                hostID = GetHostIDInternal();
            }
            System.Console.WriteLine($"Current HostId: {hostID}");
            return hostID;
        }

        internal static string GetSGHostAddress()
        {
            string zookeeperStr = ConfigurationManager.AppSettings.Get("SG.HostAddress");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "http://localhost:8008";

            return zookeeperStr;
        }

        internal static string GetHSFHostIP()
        {
            string zookeeperStr = ConfigurationManager.AppSettings.Get("HSF.HostAddress");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "127.0.0.1";

            return zookeeperStr;
        }

        internal static int GetHSFHostPort()
        {
            string zookeeperStr = ConfigurationManager.AppSettings.Get("HSF.HostPort");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "8000";

            int result = 8000;
            int.TryParse(zookeeperStr, out result);

            return result;
        }

        internal static void SetHSFHostPort(string port)
        {
            string key = "HSF.HostPort";
            //ConfigurationManager.AppSettings.Set("HSF.HostPort", port);

            var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var settings = configFile.AppSettings.Settings;
            if (settings[key] == null)
            {
                settings.Add(key, port);
            }
            else
            {
                settings[key].Value = port;
            }
            configFile.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
        }


        internal static bool IsSQLModel()
        {
            string zookeeperStr = ConfigurationManager.AppSettings.Get("HSF.IsSQLModel");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "false";
            bool result = false;
            bool.TryParse(zookeeperStr, out result);

            return result;
        }

        internal static bool IsShowServiceError()
        {
            string zookeeperStr = ConfigurationManager.AppSettings.Get("HSF.IsShowServiceError");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "false";
            bool result = false;
            bool.TryParse(zookeeperStr, out result);

            return result;
        }

        internal static bool IsRegLogService()
        {
            string zookeeperStr = ConfigurationManager.AppSettings.Get("HSF.IsRegLogService");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "true";
            bool result = false;
            bool.TryParse(zookeeperStr, out result);

            return result;
        }

        internal static string GetAppVersion()
        {
            string zookeeperStr = ConfigurationManager.AppSettings.Get("HSF.AppVersion");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "2.0";

            return zookeeperStr;
        }

        internal static bool IsEnableCustomRouter()
        {
            string tRouter = ConfigurationManager.AppSettings.Get("TRouter");

            if (string.IsNullOrEmpty(tRouter))
                return false;


            return Convert.ToBoolean(tRouter);
        }

        internal static long GetHeartBeatTimeSpan()
        {
            return manager.GetHeartBeatTimeSpan();

        }

        internal static int GetMaxWorkerThreads()
        {
            var nManager = new AppConfigManager();
            return nManager.GetMaxWorkerThreads();

        }

        internal static int GetMaxCompletionPortThreads()
        {
            var nManager = new AppConfigManager();
            return nManager.GetMaxCompletionPortThreads();

        }

        internal static int GetMinWorkerThreads()
        {
            var nManager = new AppConfigManager();
            return nManager.GetMinWorkerThreads();
        }

        internal static int GetMinCompletionPortThreads()
        {
            var nManager = new AppConfigManager();
            return nManager.GetMinCompletionPortThreads();
        }

        internal static int GetFusingTPM()
        {
            return manager.GetFusingTPM();
        }

        internal static int GetFusingCount()
        {
            return manager.GetFusingCount();
        }

        internal static int GetFusingTime()
        {
            return manager.GetFusingTime();
        }

        internal static bool IsEnableThreadPoolTimeout()
        {
            string zookeeperStr = ConfigurationManager.AppSettings.Get("HSF.ThreadPool.IsEnableTimeout");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "false";
            bool result = false;
            bool.TryParse(zookeeperStr, out result);

            return result;
        }

        internal static int GetThreadPoolShardingModel()
        {
            string zookeeperStr = ConfigurationManager.AppSettings.Get("HSF.ThreadPool.ThreadShardingModel");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "0";

            int result = 0;
            int.TryParse(zookeeperStr, out result);

            return result;
        }

        internal static int GetTThreadPoolMaxWorkThreads()
        {
            string zookeeperStr = ConfigurationManager.AppSettings.Get("HSF.ThreadPool.MaxWorkingThreadCount");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "100";

            int result = 100;
            int.TryParse(zookeeperStr, out result);

            return result;
        }

        internal static int GetTThreadPoolMinWorkThreads()
        {
            string zookeeperStr = ConfigurationManager.AppSettings.Get("HSF.ThreadPool.MinWorkingThreadCount");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "5";

            int result = 5;
            int.TryParse(zookeeperStr, out result);

            return result;
        }

        internal static bool GetTThreadPoolIsShowTrace()
        {
            string zookeeperStr = ConfigurationManager.AppSettings.Get("HSF.ThreadPool.IsShowTrace");

            if (string.IsNullOrEmpty(zookeeperStr))
                zookeeperStr = "false";

            bool result = false;
            bool.TryParse(zookeeperStr, out result);

            return result;
        }
    }
}
